package com.bort.system.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.bort.system.info.CheckOutResult;
import com.bort.system.info.DeskInfo;
import com.bort.system.info.LoginResult;
import com.bort.system.model.Cart;
import com.bort.system.model.Category;
import com.bort.system.model.Goods;
import com.bort.system.model.TGameLog;
import com.bort.system.service.*;
import com.bort.system.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/client")
public class ClientApiController {

    @Resource
    UserService userService;

    @Resource
    TDeskService deskService;

    @Resource
    CategoryService categoryService;

    @Resource
    GoodsService goodsService;

    @Resource
    AsyncService asyncService;

    /**
     * 客户端登录
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public LoginResult login(@RequestParam("userName") String userName,@RequestParam("password") String password){
        return userService.login(userName,password);
    }

    @RequestMapping(value = "/getAllDesk",method = RequestMethod.GET)
    public Result<List<DeskInfo>> getAllDesk(@RequestBody Map<String,Object> params){
        Result<List<DeskInfo>> result =new Result<>();
        List<DeskInfo> list = deskService.getAllDesk(params);
        result.setData(list);
        return result;
    }

    @RequestMapping(value = "/loadCategories",method = RequestMethod.GET)
    public Result<List<Category>> loadCategories(@RequestBody Map<String,Object> params){
        return categoryService.loadAllCategories(params);
    }

    @RequestMapping(value = "/loadGoodsByCategoryId",method = RequestMethod.GET)
    public Result<List<Goods>> loadGoodsByCategoryId(@RequestParam("categoryId") Integer categoryId){
        List<Goods> list = goodsService.loadGoodsByCategoryId(categoryId);
        return Result.success(list);
    }

    @RequestMapping(value = "/openDesk",method = RequestMethod.POST)
    public Result<TGameLog> openDesk(@RequestHeader Map<String,String> header,@RequestBody Map<String,Object> params){
        Integer userId = getUserId(header);
        JSONObject paramJson = JSONObject.parseObject(JSONObject.toJSONString(params));
        DeskInfo data = paramJson.getObject("data", DeskInfo.class);
        int hour = paramJson.getIntValue("hValue");
        int minute = paramJson.getIntValue("mValue");
        String playerName = paramJson.getString("playerName");
        TGameLog gameLog = deskService.openDesk(data, hour, minute, playerName,userId);
        return Result.success(gameLog);
    }

    @RequestMapping(value = "/checkOut",method = RequestMethod.POST)
    public CheckOutResult checkOut(@RequestBody Map<String,Object> params){
        JSONObject paramJson = JSONObject.parseObject(JSONObject.toJSONString(params));
        Date startTime = paramJson.getDate("startTime");
        Date date = paramJson.getDate("date");
        BigDecimal price = paramJson.getBigDecimal("price");
        CheckOutResult result = asyncService.checkOut(startTime, date, price);
        return result;
    }

    @RequestMapping(value = "/sale",method = RequestMethod.POST)
    public Result sale(@RequestHeader Map<String,String> header,@RequestBody Map<String,Object> params){
        JSONObject paramJson = JSONObject.parseObject(JSONObject.toJSONString(params));
        Goods goods = paramJson.getObject("indexGoods", Goods.class);
        String orderNo = paramJson.getString("orderNo");
        Integer num = paramJson.getIntValue("num");
        return asyncService.sale(goods,orderNo,num,getUserId(header));
    }

    @RequestMapping(value = "/putInCart",method = RequestMethod.POST)
    public Result putInCart(@RequestHeader Map<String,String> header,@RequestBody Map<String,Object> params){
        JSONObject paramJson = JSONObject.parseObject(JSONObject.toJSONString(params));
        Goods goods = paramJson.getObject("indexGoods", Goods.class);
        String orderNo = paramJson.getString("orderNo");
        Integer num = paramJson.getIntValue("num");
        Integer id = paramJson.getInteger("id");
        String playerName = paramJson.getString("playerName");
        asyncService.putInCart(goods, orderNo, num, id, playerName,getUserId(header));
        return Result.success();
    }


    @RequestMapping(value = "/removeCart",method = RequestMethod.POST)
    public Result removeCart(@RequestBody Map<String,Object> params){
        JSONObject paramJson = JSONObject.parseObject(JSONObject.toJSONString(params));
        Cart cart = paramJson.getObject("cart", Cart.class);
        asyncService.removeCart(cart);
        return Result.success();
    }

    @RequestMapping(value = "/addOvertime",method = RequestMethod.POST)
    public Result<TGameLog> addOvertime(@RequestHeader Map<String,String> header,@RequestBody Map<String,Object> params){
        JSONObject paramJson = JSONObject.parseObject(JSONObject.toJSONString(params));
        DeskInfo data = paramJson.getObject("data", DeskInfo.class);
        Integer value = paramJson.getInteger("value");
        Integer mValue = paramJson.getInteger("mValue");
        TGameLog gameLog = deskService.addOvertime(data, value, mValue,getUserId(header));
        return Result.success(gameLog);
    }


    @RequestMapping(value = "/closeDesk",method = RequestMethod.POST)
    public Result closeDesk(@RequestHeader Map<String,String> header,@RequestBody Map<String,Object> params){
        JSONObject paramJson = JSONObject.parseObject(JSONObject.toJSONString(params));
        DeskInfo data = paramJson.getObject("data",DeskInfo.class);
        Date closeTime = paramJson.getDate("closedTime");
        BigDecimal amount = paramJson.getBigDecimal("amount");
        BigDecimal pay = paramJson.getBigDecimal("pay");
        asyncService.closeDeskAsync(data,closeTime,amount,pay,getUserId(header));
        return Result.success();
    }

    @RequestMapping(value = "/getDeskById",method = RequestMethod.GET)
    public Result<DeskInfo> getDeskById(@RequestParam("deskId") Integer deskId){
        DeskInfo desk = deskService.getDeskById(deskId);
        return Result.success(desk);
    }

    private Integer getUserId(Map<String,String> header){
        if(header != null){
           Integer userId = StringUtils.isNotEmpty(header.get("userId"))?Integer.parseInt(header.get("userId")):0;
           return userId;
        }
        return -1;
    }

}
